package com.hua.yuan.common.permission;

import android.Manifest;
import android.app.Activity;
import android.support.annotation.NonNull;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

/**
 * @Author: richsjeson.
 * @Description: TODO()
 * @Date:Created in 2018/12/6 上午11:42.
 * @Modified By:
 */
public class PermissionModule {

    private  static PermissionModule mInstance;

    private RxPermissions mRxPermissions;

    public interface RequestPermissonCallback{

        void accept(final Boolean aBoolean);
    }

    public static PermissionModule getInstance(){

        if(mInstance==null){
            mInstance=new PermissionModule();
        }
        return mInstance;
    }

    public void init(@NonNull Activity pActivity){

        mRxPermissions=new RxPermissions(pActivity);
    }

    public void release(){
        mRxPermissions=null;
    }

    /**
     * 请求获取定位
     * @param pCallback
     */
    public void requestLocation(@NonNull final RequestPermissonCallback pCallback){
        mRxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(final Boolean aBoolean) throws Exception {
                pCallback.accept(aBoolean);
            }
        });
    }
    /**
     * 请求获取存储空间
     * @param pCallback
     */
    public void requestStorage(@NonNull final RequestPermissonCallback pCallback){
        mRxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(final Boolean aBoolean) throws Exception {
                pCallback.accept(aBoolean);
            }
        });
    }
}
